package com.qiaoyanqing.myqiubai;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class VPFragment extends Fragment implements Callback<Entity> {
   private ItemAdapter mAdapter;


    public VPFragment() {
        // Required empty public constructor
    }

    public static VPFragment getInstance(String type){
        VPFragment vpFragment=new VPFragment();
        Bundle bundle=new Bundle();
        bundle.putString("type",type);
        vpFragment.setArguments(bundle);
        return vpFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_v, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String type=getArguments().getString("type");
        BaseApplication application= (BaseApplication) getActivity().getApplication();
        application.getService().getList(type,1).enqueue(this);

        RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.recyclerview);

        mAdapter=new ItemAdapter(getContext());

        recyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
     //   Log.d("falg", "------onResponse: " + response.body().getList().get(0).getContent());
        mAdapter.addAll(response.body().getItems());

    }

    @Override
    public void onFailure(Call<Entity> call, Throwable t) {
         t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
